//
//  NetManager+UPRequest.m
//  Sports
//
//  Created by up72_01 on 15/8/27.
//  Copyright (c) 2015年 wisdom. All rights reserved.
//

#import "NetManager+UPRequest.h"
#import "FileMD5Hash.h"
#import "BMWQusetionModel.h"
@implementation NetManager (UPRequest)


#pragma mark - 登录
- (void)login:(NSString *)userName pwd:(NSString *)pwd
{
    NSMutableDictionary *argDict = [NSMutableDictionary dictionary];
    
    [argDict setObject:userName forKey:@"userName"];
    [argDict setObject:pwd forKey:@"userPassword"];
    
    [self sendPOSTRequestToServerWithURL:@"/login" postData:argDict];
}

#pragma mark - 签名协议校验
- (void)checkSingnature:(NSString *)surName phone:(NSString *)phone
{
    NSMutableDictionary *argDict = [NSMutableDictionary dictionary];
    
    [argDict setObject:[MTool getUserName] forKey:@"userName"];
    [argDict setObject:surName forKey:@"surname"];
    [argDict setObject:phone forKey:@"mobilePhone"];
    
    [self sendPOSTRequestToServerWithURL:@"/checkSignature" postData:argDict];
}

#pragma mark - 查询对应用户表格信息超链接
- (void)getInfoUrl:(NSString *)num {
    NSMutableDictionary *argDict = [NSMutableDictionary dictionary];
    
    [argDict setObject:[MTool getUserName] forKey:@"userName"];
    [argDict setObject:num forKey:@"mobilePhone"];
    [argDict setObject:[[MTool getCurrentData] objectForKey:@"dealerID"] forKey:@"dealerID"];
    
    [self sendPOSTRequestToServerWithURL:@"/getInfoUrl" postData:argDict];
}

#pragma mark - 信息上传
/**
 *  @brief     信息上传
 *  @paragram  userName 用户名 checkIn 请求的信息（二维码扫描到的信息以及手机号）
 */

- (void)signIn:(NSMutableDictionary *)mesDic
{
    NSMutableDictionary *messDic = [NSMutableDictionary dictionaryWithDictionary:mesDic];
    
    [messDic setObject:[MTool getUserName] forKey:@"userName"];

    
//    [self uploadWithUrl:@"/uploadSurvery" body:messDic method:@"POST" constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//        
//        NSString* documentsDirectory = [paths objectAtIndex:0];
//        
//        NSString *path = [NSString stringWithFormat:@"%@/%@_protocolPhoto.jpg",documentsDirectory,[mesDic objectForKey:@"mobilePhone"]];
//        
//        NSString *MD5 = [FileMD5Hash computeMD5HashOfFileInPath:path];
//        
//        if (MD5.length > 0) {
//            //MD5的添加
//            [formData appendPartWithFormData:[MD5 dataUsingEncoding:NSUTF8StringEncoding] name:@"protocolMD5Hash"];
//            
//            [formData appendPartWithFileData:[NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@_protocolPhoto.jpg",documentsDirectory,[mesDic objectForKey:@"mobilePhone"]]] name:@"protocolPhoto" fileName:[NSString stringWithFormat:@"protocol_%@.jpg",[mesDic objectForKey:@"mobilePhone"]] mimeType:@"image/jpg"];
//        }
//    
//    }];
    [self uploadWithUrl:@"/uploadSurvery" body:messDic method:@"POST" constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        
        NSString* documentsDirectory = [paths objectAtIndex:0];
        
        NSString *path = [NSString stringWithFormat:@"%@/%@_protocolPhoto.jpg",documentsDirectory,[mesDic objectForKey:@"mobilePhone"]];
        
        NSString *MD5 = [FileMD5Hash computeMD5HashOfFileInPath:path];
        
        if (MD5.length > 0) {
            //MD5的添加
            [formData appendPartWithFormData:[MD5 dataUsingEncoding:NSUTF8StringEncoding] name:@"protocolMD5Hash"];
            
            long time = [[NSDate date] timeIntervalSince1970];
            
            NSString *timeStamp = [NSString stringWithFormat:@"%ld000",time];
            
            [formData appendPartWithFileData:[NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@_protocolPhoto.jpg",documentsDirectory,[mesDic objectForKey:@"mobilePhone"]]] name:@"protocolPhoto" fileName:[NSString stringWithFormat:@"%@_%@.jpg",[mesDic objectForKey:@"mobilePhone"],timeStamp] mimeType:@"image/jpg"];
        }
        
    }];
}


#pragma mark - 版本更新
- (void)updateVersion
{
    NSMutableDictionary *argDict = [NSMutableDictionary dictionary];
    
    [argDict setObject:[MTool getUserName] forKey:@"userName"];
    
    [self sendPOSTRequestToServerWithURL:@"/checkVersion" postData:argDict];
}

@end
